package com.example.myapplication2;

import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.io.*;

public class ImageFileActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;
    private String fileName = "kk.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_file);
        findViewById(R.id.ImageFileButtonView).setOnClickListener(this);
        findViewById(R.id.ImageFileButtonSave).setOnClickListener(this);
        findViewById(R.id.ImageFileButtonView2).setOnClickListener(this);
        findViewById(R.id.ImageFileButtonView3).setOnClickListener(this);
        imageView = findViewById(R.id.imageView10);
    }

    @Override
    public void onClick(View view) {
        String dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String path=dir+ File.separatorChar+fileName;

        switch (view.getId()){
            case R.id.ImageFileButtonView:
                view(path);
                break;
            case R.id.ImageFileButtonView2:
                view2(path);
                break;
            case R.id.ImageFileButtonView3:
                view3(path);
                break;
            case R.id.ImageFileButtonSave:
                imageSave(path);
                break;
        }
    }

    private void view3(String path) {
        imageView.setImageURI(Uri.parse(path));
    }

    private void view2(String path) {
        imageView.setImageBitmap(BitmapFactory.decodeFile(path));
    }

    private void imageSave(String path){
        //从指定的资源中获取位图对象
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.button_img_k);

        FileOutputStream stream=null;
        try {
            stream=new FileOutputStream(path);
        }catch (Exception e){

        }
        // 将位图文件处理成流对象
        boolean compress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        try {
            if (stream!=null){
                stream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void view(String path) {
        FileInputStream is= null;
        try {
            is = new FileInputStream(path);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            if (is!=null){
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}